#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@File    : watermark_service.py
@Author  : Spring
@Time    : 2024/12/6 10:00
@Desc: 水印服务类
"""
import jpype

from common.common import Result, BusinessInput


# class WatermarkServiceInitializer(type):
#     """
#     水印元数据初始化类
#     """
#
#     def __init__(cls, name, bases, dct):
#         super().__init__(name, bases, dct)
#         Bootstrap.start()
#         cls.initialize()


# class WatermarkService(metaclass=WatermarkServiceInitializer):
class WatermarkService:
    """
    水印业务服务类
    """
    j_cls_structured_file_watermark = None
    j_cls_un_structured_file_watermark = None
    j_cls_business_input = None
    j_cls_file = None
    j_cls_base64 = None

    @classmethod
    def initialize(cls):
        """
        初始化
        :return:
        """
        cls.j_cls_structured_file_watermark = jpype.JClass('com.venus.dms.watermark.service.StructuredFileWatermark')
        cls.j_cls_un_structured_file_watermark = jpype.JClass('com.venus.dms.watermark.service.UnstructuredFileWatermark')
        cls.j_cls_business_input = jpype.JClass('com.venus.dms.common.entity.BusinessInput')
        cls.j_cls_file = jpype.JClass('java.io.File')
        cls.j_cls_base64 = jpype.JClass('cn.hutool.core.codec.Base64')

    @classmethod
    def struct_watermark(cls, business_input: BusinessInput, source_file_path: str, target_file_path: str):
        """
        结构化水印添加
        :param business_input: 业务输入参数
        :param source_file_path: 原文件路径
        :param target_file_path: 添加水印成功后的文件路径
        :return:
        """
        # 构建水印业务参数
        j_business_input = cls.j_cls_business_input()
        j_business_input.setIp(business_input.ip)
        j_business_input.setUser(business_input.user)
        j_business_input.setCharset(business_input.charset)
        j_business_input.setApiCustomizeWatermarkContent(business_input.api_customize_watermark_content)
        j_structured_file_watermark = cls.j_cls_structured_file_watermark(j_business_input)
        # 构建水印原始文件 目标文件
        j_source_file = cls.j_cls_file(source_file_path)
        j_target_file = cls.j_cls_file(target_file_path)
        j_watermark_result = j_structured_file_watermark.structuredFileWatermark(j_source_file, j_target_file)
        if j_watermark_result.isSuccess():
            j_water_mark_file = j_watermark_result.getData()
            j_water_mark_file_base64 = cls.j_cls_base64.encode(j_water_mark_file)
            return Result(0, data=j_water_mark_file_base64, message=j_watermark_result.getMessage())
        else:
            return Result(1, message=j_watermark_result.getMessage())

    @classmethod
    def un_struct_watermark(cls, business_input: BusinessInput, source_file_path: str, target_file_path: str):
        """
        非结构化文件
        :param business_input: 业务输入参数
        :param source_file_path: 原文件路径
        :param target_file_path: 添加水印成功后的文件路径
        :return:
        """
        # 构建水印业务参数
        j_business_input = cls.j_cls_business_input()
        j_business_input.setIp(business_input.ip)
        j_business_input.setUser(business_input.user)
        j_business_input.setCharset(business_input.charset)
        j_business_input.setApiCustomizeWatermarkContent(business_input.api_customize_watermark_content)
        j_un_structured_file_watermark = cls.j_cls_un_structured_file_watermark(j_business_input)
        # 构建水印原始文件 目标文件
        j_source_file = cls.j_cls_file(source_file_path)
        j_target_file = cls.j_cls_file(target_file_path)
        j_watermark_result = j_un_structured_file_watermark.unstructuredFileWatermark(j_source_file, j_target_file)
        if j_watermark_result.isSuccess():
            j_water_mark_file = j_watermark_result.getData()
            j_water_mark_file_base64 = cls.j_cls_base64.encode(j_water_mark_file)
            return Result(0, data=j_water_mark_file_base64, message=j_watermark_result.getMessage())
        else:
            return Result(1, message=j_watermark_result.getMessage())
